package com.zhkj.txg.module.home.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.chad.library.adapter.base.entity.node.BaseNode
import com.zhkj.lib.base.BaseViewModel
import com.zhkj.lib.http.HttpResponse
import com.zhkj.lib.http.ViewModelSubscribeListener
import com.zhkj.lib.utils.AccountManager
import com.zhkj.txg.module.home.entity.*
import com.zhkj.txg.module.home.model.HomeManager

@Suppress("UNCHECKED_CAST")
class ShopViewModel(application: Application) : BaseViewModel(application) {

    private val homeManager = HomeManager()
    private val cateLiveData = MutableLiveData<HttpResponse<ShopCategoryResponse>>()
    val shopLiveData = MutableLiveData<HttpResponse<ShopResponse>>()
    val followLiveData = MutableLiveData<HttpResponse<ShopFollowResponse>>()
    val shopAllProductLiveData = MutableLiveData<HttpResponse<SearchProductResponse>>()

    var shop: ShopEntity? = null
    private val numb = 20
    var page = 1
        private set

    fun shopDetail(token: String, shopId: Int) {
        val listener = ViewModelSubscribeListener(shopLiveData)
        val shopDetail = homeManager.shopDetail(token, shopId, listener)
        dispose.add(shopDetail)
    }

    fun followShop(token: String, shopId: Int, isCurrentFollow: Boolean) {
        val listener = object : ViewModelSubscribeListener<ShopFollowResponse>(followLiveData) {
            override fun onSuccess(resp: ShopFollowResponse) {
                shopLiveData.value?.response?.data?.let {
                    it.isCollect = !isCurrentFollow
                    it.collectCount =
                        if (isCurrentFollow) it.collectCount - 1 else it.collectCount + 1
                    resp.isFollow = it.isCollect
                    resp.followCount = it.collectCount
                }
                shop?.let {
                    it.isCollect = !isCurrentFollow
                    it.collectCount =
                        if (isCurrentFollow) it.collectCount - 1 else it.collectCount + 1
                    resp.isFollow = it.isCollect
                    resp.followCount = it.collectCount
                }
                super.onSuccess(resp)
            }
        }
        val followShop = homeManager.followShop(token, shopId, listener)
        dispose.add(followShop)
    }

    fun shopCate(shopId: Int): MutableLiveData<HttpResponse<ShopCategoryResponse>> {
        val listener = object : ViewModelSubscribeListener<ShopCategoryResponse>(cateLiveData) {
            override fun onSuccess(resp: ShopCategoryResponse) {
                for (cate in resp.data) {
                    cate.childNode = cate.child as MutableList<BaseNode>
                }
                super.onSuccess(resp)
            }
        }
        val shopCate = homeManager.shopCate(shopId, listener)
        dispose.add(shopCate)
        return cateLiveData
    }

    fun searchProduct(
        shopId: Int, restart: Boolean = false
    ) {
        val listener = object :
            ViewModelSubscribeListener<SearchProductResponse>(shopAllProductLiveData) {
            override fun onSuccess(resp: SearchProductResponse) {
                resp.hasMore = resp.data.size >= numb
                resp.loadMore = page > 1
                super.onSuccess(resp)
                page += 1
            }
        }
        if (restart) page = 1
//        val searchProduct = homeManager.searchProduct(
//            AccountManager.getToken(), shopId, 0, 0, "weigh", "asc", 0, "", page, numb, listener
//        )
        val searchProduct = homeManager.searchProduct(
            token = AccountManager.getToken(),
            shopId = shopId,
            activeType = 0,
            search = null,
            catId = 0,
            sort = "weigh",
            sortAsc = "asc",
            page = page.toString(),
            rows = numb.toString(),
            recommend = null,
            listener = listener
        )
        dispose.add(searchProduct)
    }
}